﻿/*
 * Encoding Test
 * 
 * Author: snowdreamist
 * Date: 2011/11/18
 * 
 */

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BitTorrentSharp.Tracker;
using BitTorrentSharp.BEncoding;
using BitTorrentSharp.Torrent;

namespace BitTorrentSharp.Test
{
    /// <summary>
    /// TracerUnitTest 的摘要说明
    /// </summary>
    [TestClass]
    public class TracerUnitTest
    {
        public TracerUnitTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public void SimpleTracerWebRequestTest()
        {
            TorrentFile torrent = TorrentFile.CreateFromFile(TestUtility.TestTorrentFiles[2]);

            TrackerWebRequest request = new TrackerWebRequest();

            request.AnnountUri = new Uri(torrent.MetaInfo.Announces[0], UriKind.Absolute);
            request.InfoHash = torrent.InfoHash;
            request.PeerID = new string(Enumerable.Repeat<char>('1', 20).ToArray());
            request.EnableCompact = true;
            request.Port = 9900;
            request.Left = torrent.MetaInfo.Length;
            request.Event = null;
            request.PeersCountWanted = 50;

            var req = request.CreateHttpWebRequest();

            TrackerWebResponse response;

            while (true)
            {
                try
                {
                    response = request.GetResponse();
                    break;
                }
                catch(Exception error)
                {
                    Console.WriteLine(error.Message);
                }
            }
            

            Assert.AreEqual<bool>(true, response.Peers.Exists(p => p.Id.ToString() == request.PeerID && p.Port == request.Port));
        }

        [TestMethod]
        public void TrackerReporterSimpleTest()
        {
            TorrentFile torrent = TorrentFile.CreateFromFile(TestUtility.TestTorrentFiles[2]);

            TrackerReporter reporter = new TrackerReporter(
                new string(Enumerable.Repeat<char>('1', 20).ToArray()),
                new Uri(torrent.MetaInfo.Announces[0], UriKind.Absolute),
                torrent.InfoHash,
                () => 0,
                () => 0,
                () => torrent.MetaInfo.Length,
                null,
                true);

            reporter.Reported += (s, e) =>
                {
                    Assert.IsNull(e.Error);
                };

            reporter.Report(null);
        }
    }
}
